﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Media;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Snake_Classic
{
    class Engine
    {
        Form1 form1;
        private Wall wall = new Wall();
        private Snake snake;
        private SoundPlayer player = new SoundPlayer();

        public Engine(Snake snake, Form1 form)
        {
            this.snake = snake;
            this.form1 = form;
        }

        public void PlaySound()
        {
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string Filepath = Path.Combine(directory, "Sad_Trombone.wav");
            player = new SoundPlayer(Filepath);
            player.Play();
        }

        public void PlayMusicInGame()
        {

            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string Filepath = Path.Combine(directory, "FreedomDance.wav");
            player = new SoundPlayer(Filepath);
            player.PlayLooping();
        }

        public void StopMusic()
        {
            player.Stop();
        }
        /// <summary>
        /// check the collision head of snake with another her part
        /// </summary>
        public void Collision()
        {
            for (int i = 1; i < snake.SnakeRec.Length; i++)
            {
                if (snake.SnakeRec[0].IntersectsWith(snake.SnakeRec[i]))
                {
                    PlaySound();
                    form1.Restart();
                    form1.Hide();
                    return;
                }
                else if (snake.SnakeRec[0].IntersectsWith(wall.WallRec))
                {
                    PlaySound();
                    form1.Restart();
                    form1.Hide();
                    return;
                }
            }
            if (snake.SnakeRec[0].X < 0)
            {
                snake.SnakeRec[0].X = form1.Width;
            }
            else if (snake.SnakeRec[0].X > form1.Width)
            {
                snake.SnakeRec[0].X = 0;
            }
            else if (snake.SnakeRec[0].Y < 0)
            {
                snake.SnakeRec[0].Y = form1.Height;
            }
            else if (snake.SnakeRec[0].Y > form1.Height)
            {
                snake.SnakeRec[0].Y = 0;
            }
        }
    }
}
